/**
 * 
 */
package util;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class stub {
	
	
	@Ignore
	@Test
	public void test1() throws InterruptedException{

		driver.navigate().to("https://webmail");
		driver.findElement(By.id("username")).sendKeys("RDTest");
		driver.findElement(By.id("password")).sendKeys("xyz55OS!");
		driver.findElement(By.xpath("//input[@value='Sign in']")).click();
		Thread.sleep(2000);
		driver.findElement(By.id("txtS")).sendKeys(""); //search
		driver.findElement(By.id("divSIcon")).click();
		Thread.sleep(10000);
		
     String[] title = {"Your Resilience Direct password"}; //title
  
		
		WebElement Rent_rentc =(WebElement) ((JavascriptExecutor) driver).executeScript("return document.querySelectorAll('[read=\"0\"]')[0].parentNode"); //assigning it to a webelemt
		//SeleniumWait.pause(2);
	
		String newwindowTitle = driver.getWindowHandle(); // store the main window 
		Actions builder = new Actions(driver);
	    org.openqa.selenium.interactions.Action a=builder.moveToElement(Rent_rentc).doubleClick(Rent_rentc).build(); //double click on the unread email
	    
	    a.perform();
	
	    Thread.sleep(2000);
	    

	    
	    for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);                               //swicthing to the pop-up window and maximizing it
            driver.manage().window().maximize();
        }
	    
	    Thread.sleep(10000);
	
	   
	    driver.close(); //closing the pop-up window
	    driver.switchTo().window(newwindowTitle); //swicthing back to the  parent window
	    driver.findElement(By.id("delete")).click();//to delete the email
	    
	}
	
	 private WebDriver driver;
	  private String baseUrl;
	  private boolean acceptNextAlert = true;
	  private StringBuffer verificationErrors = new StringBuffer();

	  @Before
	  public void setUp() throws Exception {
	    driver = new FirefoxDriver();
	    driver.get("https://resiliencedirect-dev.ordnancesurvey.co.uk/mapmaker.web/login");
	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  }

	  /**
	   * 
	   * cookie testing for https environment
	   * verifying wether the cookie is secured 
	   * 
	  */
	@Test 
	  public void testR() throws Exception {
		  
		  Set<Cookie> c=driver.manage().getCookies();
		  System.out.println(c);
		
	        Iterator<Cookie> iter=c.iterator();
	        while(iter.hasNext()){
	            Cookie cookie=iter.next();
	            System.out.println(cookie.getDomain()+"---"+cookie.getName()+"-----------"+cookie.getPath()+
	            		"------------"+cookie.getValue());
	            Assert.assertTrue(cookie.isSecure());
	                     
	        }}
	  

	  @After
	  public void tearDown() throws Exception {
	    driver.quit();
	    String verificationErrorString = verificationErrors.toString();
	    if (!"".equals(verificationErrorString)) {
	      //fail(verificationErrorString);
	    }
	  }
	        

	  private boolean isElementPresent(By by) {
	    try {
	      driver.findElement(by);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	  }

	  private boolean isAlertPresent() {
	    try {
	      driver.switchTo().alert();
	      return true;
	    } catch (NoAlertPresentException e) {
	      return false;
	    }
	  }

	  private String closeAlertAndGetItsText() {
	    try {
	      Alert alert = driver.switchTo().alert();
	      String alertText = alert.getText();
	      if (acceptNextAlert) {
	        alert.accept();
	      } else {
	        alert.dismiss();
	      }
	      return alertText;
	    } finally {
	      acceptNextAlert = true;
	    }
	  }

    @Ignore
	@Test
	public void test() throws InterruptedException{

		driver.navigate().to("");
		//List<String> list = new ArrayList<String>();
		
		String element = driver.findElement(By.xpath("/html/body/div[3]/div/div/section/div[3]/ul/li/ul")).getText();
		System.out.println("-----------------------");
		String[] element1 =element.split("\\n");
		String[] pagetitle = {"",};

		System.out.println(element1.length);
		
		for (int i = 0; i <element1.length; i++) {
			driver.findElement(By.linkText(element1[i])).click();
			Thread.sleep(2000);
		String title =	driver.getTitle();
		
		
		if( (i==0)){
			Assert.assertEquals(title.toLowerCase(), pagetitle[0].toLowerCase());
		}
		else {
			Assert.assertEquals(title.toLowerCase(), "used " + element1[i].toLowerCase() +" for sale in united kingdom | gumtree".toLowerCase());
			System.out.println(i);
		}
		
			driver.navigate().back();
		}
		
		System.out.println("========================");
	
       
	}

	
 
	  
	
	}

